package com.hmall.item.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfiguration {

    public static final String ITEM_TOPIC = "item.topic";
    public static final String ITEM_INSERT_QUEUE = "item.insert.queue";
    public static final String ITEM_DELETE_QUEUE = "item.delete.queue";
    public static final String ITEM_INSERT = "item.insert";
    public static final String ITEM_DELETE = "item.delete";

    /**
     * 商品主题交换机
     */
    @Bean
    public TopicExchange itemTopicExchange(){
        return ExchangeBuilder.topicExchange(ITEM_TOPIC)
                .build();
    }

    /**
     * 商品插入队列
     */
    @Bean
    public Queue itemInsertQueue(){
        return QueueBuilder.durable(ITEM_INSERT_QUEUE).build();
    }

    /**
     * 商品删除队列
     */
    @Bean
    public Queue itemDeleteQueue(){
        return QueueBuilder.durable(ITEM_DELETE_QUEUE).build();
    }

    /**
     * 商品插入队列绑定商品主题交换机
     */
    @Bean
    public Binding itemInsertQueueBindingItemTopicExchange(){
        return BindingBuilder.bind(itemInsertQueue())
                .to(itemTopicExchange())
                .with(ITEM_INSERT);
    }

    /**
     * 商品删除队列绑定商品主题交换机
     */
    @Bean
    public Binding itemDeleteQueueBindingItemTopicExchange(){
        return BindingBuilder.bind(itemDeleteQueue())
                .to(itemTopicExchange())
                .with(ITEM_DELETE);
    }
}
